<?php

namespace Application\Site1Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Doctrine\ORM\Events;

class Site1Controller extends Controller
{
    /**
     * @return EntityManager
     */
    private function getEm()
    {
        return $this->container->getDoctrine_ORM_EntityManagerService();
    }

    private function genEnt($value, $node, $confirmed, $draft, $hidden)
    {
        sleep(2);
        $entity = new \Bundle\FireIceBundle\Entities\Text();
        $entity->setVisiable($hidden);
        $entity->setField($value);
        $entity->setConfirmed($confirmed);
        $entity->setDraft($draft);
        $entity->setNode($node);

        return $entity;
    }

    public function indexAction()
    {
        $result = array();
        // Очистка таблиц
       /* $em = $this->getEm();

        //$result['DocumentHistoryDeleted'] =
        //        $em->createQuery('delete from Bundle\FireIceBundle\Entities\DocumentHistory h')->execute();
        //$result['DocumentDeleted'] =
        //        $em->createQuery('delete from Bundle\FireIceBundle\Entities\Document d')->execute();

        $result['TextDeleted'] =
                $em->createQuery('delete from Bundle\FireIceBundle\Entities\Text t')->execute();

        $entities = array();

        // Подтверждённая, видимая не черновик
        $entities['main'] = $this->genEnt('Main Original', 1, true, false, true);
        $entities['main2'] = $this->genEnt('Main2 Original', 2, true, false, true);

        $entities['main - draft1'] = $this->genEnt('Main - draft 1', 1, false, true, false);
        $entities['main - draft2'] = $this->genEnt('Main - draft 2', 1, false, true, false);
        $entities['main - draft3'] = $this->genEnt('Main - draft 3', 1, false, true, false);

        foreach($entities as $key => $entity)
        {
            \var_dump($entity);
            $em->persist($entity);
        }
        $em->flush();

        $query = $em->createQueryBuilder()
                ->select('t')
                ->from('Bundle\FireIceBundle\Entities\Text', 't');


        //$entity->setField1(rand());

        //$em->persist($entity);

//        die(\var_dump($em->getUnitOfWork()));

  //      $em->flush();
*/
        return $this->render('Site1Bundle:Default:index', array('result' => $result));
    }
}